package org.example;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

public class FileSyncServer {
    private static final String SERVER_FOLDER_PATH = "D:/clouder/server";
    private static final int SERVER_PORT = 8888;

    public static void main(String[] args) {
        try {
            // 创建服务器Socket
            ServerSocket serverSocket = new ServerSocket(SERVER_PORT);
            System.out.println("文件同步服务器已启动，监听端口：" + SERVER_PORT);

            while (true) {
                // 监听客户端连接
                Socket socket = serverSocket.accept();
                System.out.println("接收到客户端连接：" + socket.getInetAddress().getHostAddress());

                // 处理客户端请求
                handleRequest(socket);

                // 关闭连接
                socket.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    // 处理客户端请求
    private static void handleRequest(Socket socket) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String request = reader.readLine();
        String[] parts = request.split(" ");
        String action = parts[0];
        String filePath = parts[1];

        String response = "";
        if (action.equals("CREATE")) {
            response = createFile(filePath);
        } else if (action.equals("UPDATE")) {
            response = updateFile(filePath);
        } else if (action.equals("DELETE")) {
            response = deleteFile(filePath);
        }

        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        writer.write(response);
        writer.newLine();
        writer.flush();
    }

    // 创建文件
    private static String createFile(String filePath) {
        Path fileToCreate = Path.of(SERVER_FOLDER_PATH, filePath);
        try {
            Files.createFile(fileToCreate);
            return "文件已成功创建：" + fileToCreate.toString();
        } catch (IOException ex) {
            return "创建文件时发生错误：" + ex.getMessage();
        }
    }

    // 更新文件
    private static String updateFile(String filePath) {
        Path sourcePath = Path.of(SERVER_FOLDER_PATH, filePath);
        Path destinationPath = Path.of(SERVER_FOLDER_PATH, filePath + ".bak");
        try {
            Files.move(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING);
            return "文件已成功更新：" + sourcePath.toString();
        } catch (IOException ex) {
            return "更新文件时发生错误：" + ex.getMessage();
        }
    }

    // 删除文件
    private static String deleteFile(String filePath) {
        Path fileToDelete = Path.of(SERVER_FOLDER_PATH, filePath);
        try {
            Files.deleteIfExists(fileToDelete);
            return "文件已成功删除：" + fileToDelete.toString();
        } catch (IOException ex) {
            return "删除文件时发生错误：" + ex.getMessage();
        }
    }
}
